//
//  SearchHotWordCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/26.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "SearchHotWordCell.h"

static NSInteger baseButtonTag = 290310923;
static CGFloat buttonOffset = 15;
static CGFloat buttonItemOffset = 10;
static CGFloat buttonHeight = 30;

@interface SearchHotWordCell ()

@end

@implementation SearchHotWordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (CGFloat)getHeightWithArr:(NSArray *)arr {
    CGFloat height = 0 ;
    
    
    
    CGFloat w = 0;
    CGFloat number = 0;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *str = [arr safeObjectAtIndex:i];
        
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont14};
        CGFloat length = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        // 先判断是否超出屏幕
        if (w + length + buttonOffset * 2 + buttonItemOffset * 2 > ScreenWidth) {
            number += 1;
            w = 0;
        }
        w = buttonItemOffset + w + (length + buttonOffset * 2);
    }
    if (arr.count != 0) {
        height = (number + 1) * (buttonItemOffset + buttonHeight);
    }
    
    height += buttonOffset ;// 添加上边的距离
    
    return height;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setTitlesArr:(NSArray *)arr{
    [self removeAllSubviews];
    
    CGFloat w = buttonOffset - buttonItemOffset;
    CGFloat origin_y = buttonOffset;
    NSInteger row = 0;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *str = [arr safeObjectAtIndex:i];
        
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont14};
        CGFloat length = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        
        // 先判断是否超出屏幕
        if (w + length + buttonOffset * 2 + buttonItemOffset * 2 > ScreenWidth) {
            row += 1;
            w = buttonOffset - buttonItemOffset;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((buttonItemOffset + w), row * (buttonItemOffset + buttonHeight) + origin_y, (length + buttonOffset * 2), buttonHeight);
        button.tag = i + baseButtonTag;
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
        button.titleLabel.font = TextNormalFont14;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:[UIColor grayF9Color]];
        [self addSubview:button];
        
        w = button.origin.x + button.width;
        
    }
    
}

- (void)buttonClick:(UIButton *)sender {
    if (self.block) {
        NSInteger index = sender.tag - baseButtonTag;
        self.block(index);
    }
}

@end
